写在前面
鉴于网上教程可用性不高,为此写下教程.
此教程适合于广大Mac(全平台)用户,Windows用户可选宇宙无敌的VS.(划掉)
(更新) Windows试用llvm和gdb实现调试.
必要的组件
1 | Xcode command tools |
安装Xcode command tools
终端键入
1 | xcode-select --install |
安装cpptools
配置VSC
c_cpp_properties.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19{
"configurations": [
{
"name": "Mac",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"macFrameworkPath": [
"/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks"
],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64"
}
],
"version": 4
}launch.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/Debug/${fileBasenameNoExtension}.out",
"args": [],
"stopAtEntry": false,
"preLaunchTask": "build",
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true,
"MIMode": "lldb",
// "miDebuggerPath": "/etc/bin",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}tasks.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "clang++",
"type": "shell",
"args": [
"-g","-o","Debug/${fileBasenameNoExtension}.out","${file}",
"-std=c++11",
],
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "shared"
}
}
],
}配置完成 临时文件将保存在Debug文件夹中
安装LLVM
LLVM Download Page,在网页中找到适用于Windows
64位的最新预编译版本,不需要下载sig签名文件。安装过程中注意选择为所有用户安装,这样会为你添加到环境变量。
这两步完成以后打开cmd,输入clang应该可以看到如下输出。
安装MinGW-w64
MinGW-w64 - for 32 and 64 bit Windows,安装时注意选择体系架构为x86_64。由于网络原因,你可能不能把它下载下来,经过一点探索,安装程序需要下载一个叫做x86_64-7.1.0-release-posix-seh-rt_v5-rev2
的文件,其实我们可以直接在SourceForge上搜到这个MinGW-w64 - for 32 and 64 bit Windows,到里面选择第一个下载。下载完成后解压里面的mingw64文件夹中的内容到你安装LLVM的同一个目录合并,合并里面所有文件夹,不会有冲突。
打开终端验证是否能够打开gdb.exe.
配置vscode
launch.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch in Windows",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"preLaunchTask": "BuildInWindows",
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": false
}
]
},
]
}
- tasks.json
1 | { |
最后成功调试!